package org.imageMaker.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.sun.org.apache.xerces.internal.parsers.DOMParser;

public class Parser {
	public static Map<String,Coords> start(String source) throws FileNotFoundException, SAXException, IOException{
		
		Map<String,Coords> ret = new HashMap<String,Coords>();
		
		// Create the parser  
		DOMParser parser = new DOMParser();  
		// Process XML file  
		parser.parse(new InputSource( new FileInputStream(source)));  
		// Obtaint Document object  
		Document doc = parser.getDocument();
		
		// Obtain root tag  
		Element rootElem = doc.getDocumentElement();  
		// Iteration over child
		NodeList children = rootElem.getChildNodes();
		for(int i=0;i<children.getLength();i++){  
		   Node node = children.item(i);  
		   if (node instanceof Element){
			   String nodeName = node.getNodeName();
			   if (nodeName.equals("node")){
				   NamedNodeMap nnm = node.getAttributes();
				   for (int j = 0;j<nnm.getLength();j++){
					   Coords coord = new Coords(	nnm.getNamedItem("x").getNodeValue(),
							   						nnm.getNamedItem("y").getNodeValue(),
							   						nnm.getNamedItem("height").getNodeValue(),
							   						nnm.getNamedItem("width").getNodeValue());
					   ret.put(nnm.getNamedItem("name").getNodeValue(), coord);
				   }
			   }
		   }  
		}  
		return ret;
	}
	
public static Map<String,Coords> start(InputStream source) throws FileNotFoundException, SAXException, IOException{
		
		Map<String,Coords> ret = new HashMap<String,Coords>();
		
		// Create the parser  
		DOMParser parser = new DOMParser();  
		// Process XML file  
		parser.parse(new InputSource( source ));  
		// Obtaint Document object  
		Document doc = parser.getDocument();
		
		// Obtain root tag  
		Element rootElem = doc.getDocumentElement();  
		// Iteration over child
		NodeList children = rootElem.getChildNodes();
		for(int i=0;i<children.getLength();i++){  
		   Node node = children.item(i);  
		   if (node instanceof Element){
			   String nodeName = node.getNodeName();
			   if (nodeName.equals("node")){
				   NamedNodeMap nnm = node.getAttributes();
				   for (int j = 0;j<nnm.getLength();j++){
					   Coords coord = new Coords(	nnm.getNamedItem("x").getNodeValue(),
							   						nnm.getNamedItem("y").getNodeValue(),
							   						nnm.getNamedItem("height").getNodeValue(),
							   						nnm.getNamedItem("width").getNodeValue());
					   ret.put(nnm.getNamedItem("name").getNodeValue(), coord);
				   }
			   }
		   }  
		}  
		return ret;
	}
	
	public static void main(String [] args) throws FileNotFoundException, SAXException, IOException{
		Parser.start("C:/.prorrogaPlazos.gpd.xml");
	}
}
